import O1HD from "../../../engines/gameEngine";
import level_PeopleArcheryUnitBase from "../base/level_PeopleArcheryUnitBase";
import { peopleArcheryUnitTag } from "../const/peopleArcheryConst";

const {ccclass, property} = cc._decorator;

@ccclass
export default class level_peopleArcheryOption extends level_PeopleArcheryUnitBase {

    /**数据 */
    data:any = null;

    lastWaveFlag:boolean = false;

    firstFlag:boolean = false;

    @property({type:cc.Label,tooltip:"显示文案"})
    lbText:cc.Label = null;

    @property({type:cc.Sprite,tooltip:"显示图片"})
    showSp:cc.Sprite = null;

    @property({type:cc.SpriteFrame,tooltip:"显示的图片"})
    normalShowSps:cc.SpriteFrame[] = [];

    @property({type:cc.SpriteFrame,tooltip:"仙魔显示的图片"})
    xianMoShowSps:cc.SpriteFrame[] = [];

    @property({type:cc.SpriteFrame,tooltip:"魔显示的图片"})
    moShowSps:cc.SpriteFrame[] = [];

    @property({type:cc.SpriteFrame,tooltip:"仙显示的图片"})
    xianShowSps:cc.SpriteFrame[] = [];

    @property({type:cc.SpriteFrame,tooltip:"渡劫显示的门"})
    dujieShowSps:cc.SpriteFrame[] = [];

    @property({type:cc.Node,tooltip:"渡劫标题"})
    dujieTitle:cc.Node = null;

    @property({type:cc.SpriteFrame,tooltip:"加属性的箭头图片"})
    arrowAddSpf:cc.SpriteFrame[] = [];

    @property({type:cc.SpriteFrame,tooltip:"减属性的箭头图片"})
    arrowSubSpf:cc.SpriteFrame[] = [];

    @property({type:cc.Sprite,tooltip:"箭头显示节点"})
    arrowSp:cc.Sprite = null;

    /*
     *选择门类型
     * 0 普通门 1仙门 2魔门 
    */
    optionType:string = "0";

    colorAry:cc.Color[] = [
        cc.color(24,91,153),
        cc.color(204,41,109),
    ];

    groupOption:cc.Node[] = [];

    wavaData:any = null;

    groupIndex:number = 0;

    surviveCatastropheData:any = null;

    /**
     * 绑定数据
     * @param data 数据 
     * @param lastWaveFlag  是否是最一波
     * @param type 门类型
     * @param firstFlag 是否是第一扇变身的门
     */
    bindData(data:any,lastWaveFlag:boolean,type:string,firstFlag:boolean,groupIndex:number,waveData:any,surviveCatastropheData:any){
        this.optionType = type;
        this.data = data;
        this.firstFlag = firstFlag;
        this.lastWaveFlag = lastWaveFlag;
        this.groupIndex = groupIndex;
        this.wavaData = waveData;
        this.surviveCatastropheData = surviveCatastropheData;
        let dujieTitle:cc.Label = this.dujieTitle.getChildByName("lbTitle").getComponent(cc.Label);
        dujieTitle.string = this.surviveCatastropheData ? `${this.surviveCatastropheData.surviveCatastrophe_name}` :"";
        this.refrenshOption();
    }

    protected start(): void {
        this.dujieTitle.active = this.surviveCatastropheData && this.node.x <= 0;
    }
    
    /**刷新选项 */
    refrenshOption(){
        switch(this.optionType){
            case "0":{
                let str:string = O1HD.getManagers().peopleArcheryManager.parseDataAsOptionText(this.data)// +"\n" + O1HD.getManagers().peopleArcheryManager.parseDataAsParamText(this.data);
                this.lbText.string = str;
                let param = O1HD.getManagers().peopleArcheryManager.parseDataAsParam(this.data);
                if(param >= 0){
                    this.arrowSp.spriteFrame = this.arrowAddSpf[Number(this.data.arrow_level)];
                    this.showSp.spriteFrame = this.normalShowSps[0];
                    this.lbText.node.getComponent(cc.LabelOutline).color = this.colorAry[0];
                }else{
                    this.showSp.spriteFrame = this.normalShowSps[1];
                    this.arrowSp.spriteFrame = this.arrowSubSpf[Number(this.data.arrow_level)];
                    this.lbText.node.getComponent(cc.LabelOutline).color = this.colorAry[1];
                }
                break;
            }
            case "1":{
                this.lbText.node.x = 0;
                if(this.firstFlag){
                    this.showSp.spriteFrame = this.xianMoShowSps[0];
                    this.lbText.string = "";
                }else{
                    this.showSp.spriteFrame = this.xianMoShowSps[0];
                    let param = O1HD.getManagers().peopleArcheryManager.parseDataAsParam(this.data);
                    if(param >= 0){  
                        this.showSp.spriteFrame = this.xianShowSps[0];
                        this.lbText.node.getComponent(cc.LabelOutline).color = this.colorAry[0];
                    }else{
                        this.showSp.spriteFrame = this.xianShowSps[0];
                        this.lbText.node.getComponent(cc.LabelOutline).color = this.colorAry[1];
                    }
                    let str:string = O1HD.getManagers().peopleArcheryManager.parseDataAsOptionText(this.data) +"\n" + O1HD.getManagers().peopleArcheryManager.parseDataAsParamText(this.data);
                    this.lbText.string = str;
                }
                break;
            }
            case "2":{
                this.lbText.node.x = 0;
                if(this.firstFlag){
                    this.showSp.spriteFrame = this.xianMoShowSps[1];
                    this.lbText.string = "";
                }else{
                    let param = O1HD.getManagers().peopleArcheryManager.parseDataAsParam(this.data);
                    if(param >= 0){
                        this.showSp.spriteFrame = this.moShowSps[0];
                        this.lbText.node.getComponent(cc.LabelOutline).color = this.colorAry[0];
                    }else{
                        this.showSp.spriteFrame = this.moShowSps[1];
                        this.lbText.node.getComponent(cc.LabelOutline).color = this.colorAry[1];
                    }
                    let str:string = O1HD.getManagers().peopleArcheryManager.parseDataAsOptionText(this.data) +"\n" + O1HD.getManagers().peopleArcheryManager.parseDataAsParamText(this.data);
                    this.lbText.string = str;
                }
                break;
            }
        }
        if(!this.surviveCatastropheData){
            return;
        }
        let optionRealType : string = this.surviveCatastropheData.option_Type;
        let chats:string[] = this.surviveCatastropheData["surviveCatastrophe_option_chats"].split("|");
        let chat = chats[this.groupIndex];
        switch(optionRealType){
            case "2":{
                this.showSp.spriteFrame = this.dujieShowSps[0];
                this.lbText.string = chat;
                this.lbText.node.getComponent(cc.LabelOutline).color = cc.color(135,10,1);                
                this.arrowSp.spriteFrame = null;
                break;
            }
            case "3":{
                this.showSp.spriteFrame = this.dujieShowSps[1];
                this.lbText.string = chat;
                this.lbText.node.getComponent(cc.LabelOutline).color = cc.color(3,39,59);
                this.arrowSp.spriteFrame = null;
                break;
            }
            case "4":{
                this.showSp.spriteFrame = this.dujieShowSps[2];
                this.lbText.string = chat;
                this.lbText.node.getComponent(cc.LabelOutline).color = cc.color(1,98,151);
                this.arrowSp.spriteFrame = null;
                break;
            }
            case "5":{
                this.showSp.spriteFrame = this.dujieShowSps[3];
                this.lbText.string = chat;
                this.lbText.node.getComponent(cc.LabelOutline).color = cc.color(237,55,125);
                this.arrowSp.spriteFrame = null;
                break;
            }
        }

    }

    update(dt: number): void {
        if(this.level.gamePauseFlag){
            return;
        }
        let moveDis = this.level.getRoleMoveSpd() * dt;
        this.node.y -= moveDis;
        this.autoDeath();
        super.update(dt);
    }

    onCollisionEnter(other: cc.Collider, self: cc.Collider): void {
        if(other.tag == peopleArcheryUnitTag.people){
            let param = O1HD.getManagers().peopleArcheryManager.parseDataAsParam(this.data);
            if(param >= 0){
                O1HD.getManagers().soundMgr.playSound("peopleArcherySound/Door");
            }else{
                O1HD.getManagers().soundMgr.playSound("peopleArcherySound/RedDoor");
            }
            if(this.lastWaveFlag){
                // this.level.gameWin();
                // if(this.lastWaveFlag){
                // 打开获得修为界面
                this.level.openMoneyPage();
            }
        }
    }

    clearGroupOption(){
        for(let option of this.groupOption){
            if(this.node.uuid != option.uuid){
                let os = option.getComponent(level_peopleArcheryOption);
                os.data = null;
                os.unitDeath();
            }
        }
    }

}
